package exam.test9;

import java.util.ArrayList;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Endstart
 * Date: 2020-11-03
 * Time: 9:19
 */
public class Life {
    private boolean[][] map;
    private int width, height;

    //
    public Life(int width, int height, double probability) {
        this.width = width;
        this.height = height;
        this.map = new boolean[width][height];
        initializeMap(probability);
    }

    private void initializeMap(double probability) {
        Random random = new Random();
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                if (random.nextDouble() <= probability) {
                    map[i][j] = true;
                } else {
                    map[i][j] = false;
                }
            }
        }
    }

    public void nextGeneration() {
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                int neighbours = numNeighbours(i, j);
                if (neighbours <= 1 || neighbours > 3) {
                    map[i][j] = false;
                } else {
                    if (map[i][j]) {
                        if (neighbours >= 2 && neighbours <= 3) {
                            map[i][j] = true;
                        }
                    } else if (neighbours == 3) {
                        map[i][j] = true;
                    }
                }
            }
        }
    }

    private int numNeighbours(int i, int j) {
        return 0;
    }

    public void doProcess(String param) throws IllegalArgumentException {

    }

    public void test() {
        try {
            doProcess("1234");
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }

    public int[] minMaxInd(int[] nums) {
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < nums.length; i++) {
            int value = nums[i];
            if (value > max) {
                max = value;
            }
            if (value < min) {
                min = value;
            }
        }
        int[] result = new int[2];
        result[0] = min;
        result[1] = max;

        return result;
    }

    public void removeOdds(ArrayList<Integer> nums) {
        int values[] = new int[nums.size()];
        int count = 0;
        for (Integer integer : nums) {
            if (integer % 2 == 1 || integer % 2 == -1) {
                values[count++] = integer;
            }
        }
        for (int i = 0; i < count; i++) {
            Integer value = values[i];
            nums.remove(value);
        }
    }

    public boolean isPrime(int a) {
        for (int i = 2; i <= Math.sqrt(a); i++) {
            if (a % i == 0) {
                return true;
            }
        }
        return false;
    }

    public boolean isSorted(int[] nums) {
        for (int i = 0; i < nums.length - 1; i++) {
            if (nums[i] > nums[i + 1]) {
                return false;
            }
        }
        return true;
    }

    public void swapAdjacent(int[] nums) {
        for (int i = 0; i < nums.length / 2; i++) {
            int numOne = nums[2 * i];
            int numTwo = nums[2 * i + 1];
            nums[2 * i] = numTwo;
            nums[2 * i + 1] = numOne;
        }
    }
}
